<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>学员管理</title>
    <style>
        .action-buttons .btn {
            margin-right: 5px;
        }
        .modal-header {
            background-color: #007bff;
            color: white;
        }
        .table-actions {
            width: 150px;
            text-align: center;
        }
    </style>
</head>
<body>
<jsp:include page="layout/header.jsp"/>
<div class="container mt-4">
    <div class="row">
        <div class="col-12">
            <div class="card">
                <div class="card-header d-flex justify-content-between align-items-center">
                    <h5 class="mb-0">学员管理</h5>
                    <button type="button" class="btn btn-success" data-toggle="modal" data-target="#addStudentModal">
                        <i class="fas fa-plus"></i> 新增学员
                    </button>
                </div>
                <div class="card-body">
                    <!-- 搜索表单 -->
                    <form class="form-inline mb-4" action="${pageContext.request.contextPath}/student/list.do" method="get">
                        <div class="input-group">
                            <input class="form-control" type="text" name="name" placeholder="搜索学员姓名" value="${name}"/>
                            <div class="input-group-append">
                                <button class="btn btn-primary" type="submit">
                                    <i class="fas fa-search"></i> 查询
                                </button>
                                <a class="btn btn-secondary" href="${pageContext.request.contextPath}/student/list.do">
                                    <i class="fas fa-times"></i> 清空
                                </a>
                            </div>
                        </div>
</form>
                    
                    <!-- 错误提示 -->
                    <c:if test="${not empty error}">
                        <div class="alert alert-danger alert-dismissible fade show" role="alert">
                            ${error}
                            <button type="button" class="close" data-dismiss="alert">
                                <span>&times;</span>
                            </button>
                        </div>
                    </c:if>
                    
                    <!-- 数据表格 -->
                    <div class="table-responsive">
                        <table class="table table-hover">
                            <thead class="thead-light">
    <tr>
        <th>ID</th>
                                    <th>学员姓名</th>
        <th>性别</th>
        <th>手机号</th>
        <th>学历</th>
                                    <th>所属班级</th>
                                    <th class="table-actions">操作</th>
    </tr>
                            </thead>
                            <tbody>
    <c:forEach var="s" items="${list}">
        <tr>
            <td>${s.id}</td>
            <td>${s.name}</td>
            <td><c:out value="${s.gender == 1 ? '男' : '女'}"/></td>
            <td>${s.phone}</td>
            <td>
                <c:choose>
                    <c:when test="${s.degree == 1}">初中</c:when>
                    <c:when test="${s.degree == 2}">高中</c:when>
                    <c:when test="${s.degree == 3}">大专</c:when>
                    <c:when test="${s.degree == 4}">本科</c:when>
                    <c:when test="${s.degree == 5}">硕士</c:when>
                    <c:when test="${s.degree == 6}">博士</c:when>
                    <c:otherwise>未知</c:otherwise>
                </c:choose>
            </td>
            <td>${s.clazzName}</td>
                                        <td class="action-buttons">
                                            <button type="button" class="btn btn-sm btn-primary" 
                                                    onclick="editStudent(${s.id}, '${s.name}', '${s.no}', ${s.gender}, '${s.phone}', '${s.idCard}', ${s.isCollege}, '${s.address}', ${s.degree}, '${s.graduationDate}', ${s.clazzId})" 
                                                    data-toggle="modal" data-target="#editStudentModal">
                                                <i class="fas fa-edit"></i> 编辑
                                            </button>
                                            <button type="button" class="btn btn-sm btn-danger" 
                                                    onclick="deleteStudent(${s.id}, '${s.name}')">
                                                <i class="fas fa-trash"></i> 删除
                                            </button>
            </td>
        </tr>
    </c:forEach>
                            </tbody>
 </table>
                    </div>
                    
                    <div class="mt-3">
                        <a class="btn btn-secondary" href="../pages/main.jsp">
                            <i class="fas fa-arrow-left"></i> 返回首页
                        </a>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- 新增学员弹窗 -->
<div class="modal fade" id="addStudentModal" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">新增学员</h5>
                <button type="button" class="close" data-dismiss="modal">
                    <span>&times;</span>
                </button>
            </div>
            <form action="${pageContext.request.contextPath}/student/add.do" method="post">
                <div class="modal-body">
                    <div class="row">
                        <div class="col-md-6">
                            <div class="form-group">
                                <label for="addStudentName">学员姓名 <span class="text-danger">*</span></label>
                                <input type="text" class="form-control" id="addStudentName" name="name" 
                                       placeholder="请输入学员姓名" required>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-group">
                                <label for="addStudentNo">学号</label>
                                <input type="text" class="form-control" id="addStudentNo" name="no" 
                                       placeholder="请输入学号">
                            </div>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-md-6">
                            <div class="form-group">
                                <label for="addStudentGender">性别 <span class="text-danger">*</span></label>
                                <select class="form-control" id="addStudentGender" name="gender" required>
                                    <option value="">请选择性别</option>
                                    <option value="1">男</option>
                                    <option value="2">女</option>
                                </select>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-group">
                                <label for="addStudentPhone">手机号</label>
                                <input type="text" class="form-control" id="addStudentPhone" name="phone" 
                                       placeholder="请输入手机号">
                            </div>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-md-6">
                            <div class="form-group">
                                <label for="addStudentIdCard">身份证号</label>
                                <input type="text" class="form-control" id="addStudentIdCard" name="idCard" 
                                       placeholder="请输入身份证号">
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-group">
                                <label for="addStudentIsCollege">是否在校</label>
                                <select class="form-control" id="addStudentIsCollege" name="isCollege">
                                    <option value="">请选择</option>
                                    <option value="1">是</option>
                                    <option value="0">否</option>
                                </select>
                            </div>
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="addStudentAddress">地址</label>
                        <input type="text" class="form-control" id="addStudentAddress" name="address" 
                               placeholder="请输入地址">
                    </div>
                    <div class="row">
                        <div class="col-md-6">
                            <div class="form-group">
                                <label for="addStudentDegree">学历</label>
                                <select class="form-control" id="addStudentDegree" name="degree">
                                    <option value="">请选择学历</option>
                                    <option value="1">初中</option>
                                    <option value="2">高中</option>
                                    <option value="3">大专</option>
                                    <option value="4">本科</option>
                                    <option value="5">硕士</option>
                                    <option value="6">博士</option>
                                </select>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-group">
                                <label for="addStudentGradDate">毕业日期</label>
                                <input type="date" class="form-control" id="addStudentGradDate" name="graduationDateStr">
                            </div>
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="addStudentClazz">所属班级</label>
                        <select class="form-control" id="addStudentClazz" name="clazzId">
                            <option value="">请选择班级</option>
                            <c:forEach var="c" items="${clazzList}">
                                <option value="${c.id}">${c.name}</option>
                            </c:forEach>
                        </select>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">取消</button>
                    <button type="submit" class="btn btn-success">保存</button>
                </div>
            </form>
        </div>
    </div>
</div>

<!-- 编辑学员弹窗 -->
<div class="modal fade" id="editStudentModal" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">编辑学员</h5>
                <button type="button" class="close" data-dismiss="modal">
                    <span>&times;</span>
                </button>
            </div>
            <form action="${pageContext.request.contextPath}/student/update.do" method="post">
                <input type="hidden" id="editStudentId" name="id">
                <div class="modal-body">
                    <div class="row">
                        <div class="col-md-6">
                            <div class="form-group">
                                <label for="editStudentName">学员姓名 <span class="text-danger">*</span></label>
                                <input type="text" class="form-control" id="editStudentName" name="name" 
                                       placeholder="请输入学员姓名" required>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-group">
                                <label for="editStudentNo">学号</label>
                                <input type="text" class="form-control" id="editStudentNo" name="no" 
                                       placeholder="请输入学号">
                            </div>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-md-6">
                            <div class="form-group">
                                <label for="editStudentGender">性别 <span class="text-danger">*</span></label>
                                <select class="form-control" id="editStudentGender" name="gender" required>
                                    <option value="">请选择性别</option>
                                    <option value="1">男</option>
                                    <option value="2">女</option>
                                </select>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-group">
                                <label for="editStudentPhone">手机号</label>
                                <input type="text" class="form-control" id="editStudentPhone" name="phone" 
                                       placeholder="请输入手机号">
                            </div>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-md-6">
                            <div class="form-group">
                                <label for="editStudentIdCard">身份证号</label>
                                <input type="text" class="form-control" id="editStudentIdCard" name="idCard" 
                                       placeholder="请输入身份证号">
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-group">
                                <label for="editStudentIsCollege">是否在校</label>
                                <select class="form-control" id="editStudentIsCollege" name="isCollege">
                                    <option value="">请选择</option>
                                    <option value="1">是</option>
                                    <option value="0">否</option>
                                </select>
                            </div>
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="editStudentAddress">地址</label>
                        <input type="text" class="form-control" id="editStudentAddress" name="address" 
                               placeholder="请输入地址">
                    </div>
                    <div class="row">
                        <div class="col-md-6">
                            <div class="form-group">
                                <label for="editStudentDegree">学历</label>
                                <select class="form-control" id="editStudentDegree" name="degree">
                                    <option value="">请选择学历</option>
                                    <option value="1">初中</option>
                                    <option value="2">高中</option>
                                    <option value="3">大专</option>
                                    <option value="4">本科</option>
                                    <option value="5">硕士</option>
                                    <option value="6">博士</option>
                                </select>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-group">
                                <label for="editStudentGradDate">毕业日期</label>
                                <input type="date" class="form-control" id="editStudentGradDate" name="graduationDateStr">
                            </div>
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="editStudentClazz">所属班级</label>
                        <select class="form-control" id="editStudentClazz" name="clazzId">
                            <option value="">请选择班级</option>
                            <c:forEach var="c" items="${clazzList}">
                                <option value="${c.id}">${c.name}</option>
                            </c:forEach>
                        </select>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">取消</button>
                    <button type="submit" class="btn btn-primary">保存</button>
                </div>
            </form>
        </div>
    </div>
</div>

<script src="https://cdn.jsdelivr.net/npm/jquery@3.5.1/dist/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/js/bootstrap.bundle.min.js"></script>
<script src="https://kit.fontawesome.com/a076d05399.js"></script>
<script>
    function editStudent(id, name, no, gender, phone, idCard, isCollege, address, degree, graduationDate, clazzId) {
        document.getElementById('editStudentId').value = id;
        document.getElementById('editStudentName').value = name;
        document.getElementById('editStudentNo').value = no || '';
        document.getElementById('editStudentGender').value = gender || '';
        document.getElementById('editStudentPhone').value = phone || '';
        document.getElementById('editStudentIdCard').value = idCard || '';
        document.getElementById('editStudentIsCollege').value = isCollege || '';
        document.getElementById('editStudentAddress').value = address || '';
        document.getElementById('editStudentDegree').value = degree || '';
        document.getElementById('editStudentGradDate').value = graduationDate || '';
        document.getElementById('editStudentClazz').value = clazzId || '';
    }
    
    function deleteStudent(id, name) {
        if (confirm('确认删除学员 "' + name + '" 吗？')) {
            window.location.href = 'delete.do?id=' + id;
        }
    }
</script>

<jsp:include page="layout/footer.jsp"/>
</body>
</html>

